/**
*	Group: Y1
*	The WindowManager class handles the focus on the various screens - GamePanel, LobbyPanel, and MenuPanel
*	@author Mason Kwiat
*/
package lightspeed.Media.Graphics;

import lightspeed.LightSpeed2000;
import lightspeed.Network.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class WindowManager extends JLayeredPane
{
	/**
	* Initializes the three screens that will display all action in the game
	**/
	public static GamePanel gamePanel;
	public static LobbyPanel lobbyPanel;
	public static MenuPanel menuPanel;
	
	/**
	* Creates ints for screen resolution from the LightSpeed2000 class
	**/
	public static int WINDOW_WIDTH = LightSpeed2000.w;
	public static int WINDOW_HEIGHT = LightSpeed2000.h;
	/**
	* Sets static constats for setting current panel
	**/
	public static final int GAME_PANEL = 1, LOBBY_PANEL = 2, MENU_PANEL = 3;
	public static int currentPanel;
	/**
	* Initializes the client and server
	**/
	public static LightClient client;
	public static LightServer server;
	
	/**
	*	Constructor: creates an instance of WindowManager
	*	The three panels are created, and KeyInput and MouseInput are created.
	*/
	public WindowManager()
	{	
		setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
		setFocusable(true);
		
		System.out.println(WINDOW_WIDTH + ":" + WINDOW_HEIGHT);
		
		client = null;
		server = null;
		
		//FOR TESTING ONLY
		/*
		System.out.println("win man initialized");
		this.new ServerStarter().start();	//only create server if hosting (specified in lobby panel)
		System.out.println("server started up");
		client = new LightClient("127.0.0.1", "frodo");	//will read inputs from lobby panel
		//*/
		
		//initialize and add panels
		gamePanel = new GamePanel();
		gamePanel.setBounds(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
		gamePanel.setFocusable(true);
		gamePanel.setVisible(true);
		
		lobbyPanel = new LobbyPanel(this);
		lobbyPanel.setBounds(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
		lobbyPanel.setFocusable(true);
		lobbyPanel.setVisible(true);
		
		menuPanel = new MenuPanel();
		menuPanel.setBounds(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
		menuPanel.setFocusable(true);
		menuPanel.setVisible(true);
		
		add(gamePanel);
		add(lobbyPanel);
		add(menuPanel);
		//
		
		//set menu to top layer
		setLayer(gamePanel, 1);
		setLayer(lobbyPanel, 2);
		setLayer(menuPanel, 4);
		
		lobbyPanel.removeComponents();
		
		currentPanel = MENU_PANEL;
		//
		
		addKeyListener(new KeyInput(this));
		
		addMouseMotionListener(new MouseInput(this));
		addMouseListener(new MouseInput(this));
	}
	
	/**
	*	Sets the current layer to the top (visible) layer
	*	@param panel int the integer with the current panel
	*/
	public void setTopLayer(int panel)
	{
		if (panel == MENU_PANEL)
		{
			setLayer(gamePanel, 1);
			setLayer(lobbyPanel, 2);
			setLayer(menuPanel, 4);
			
			lobbyPanel.removeComponents();
			
			currentPanel = MENU_PANEL;
		}
		else if (panel == LOBBY_PANEL)
		{
			setLayer(gamePanel, 1);
			setLayer(lobbyPanel, 4);
			setLayer(menuPanel, 3);
			
			lobbyPanel.addComponents();
			
			currentPanel = LOBBY_PANEL;
		}
		else if (panel == GAME_PANEL)
		{
			setLayer(gamePanel, 4);
			setLayer(lobbyPanel, 2);
			setLayer(menuPanel, 3);
			
			lobbyPanel.removeComponents();
			
			currentPanel = GAME_PANEL;
		}
	}

	/**
	*	Starts the server
	**/
	public void startServer()
	{
		this.new ServerStarter(this).start();
	}
	//private classes//


	/**
	*	Separate thread for server
	**/
	private class ServerStarter extends Thread
	{
		private WindowManager winman;
		
		private ServerStarter(WindowManager wm)
		{
			winman = wm;
		}
		
		public void run()
		{
			try
			{
				server = new LightServer(winman);
			}
			catch(Exception e)
			{
				System.err.println("Could not create server");
			}
		}
	}
}
